/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Menu.java
 *
 * Created on 2012.02.13., 19:51:19
 */
package launcher;

/**
 *
 * @author Tomi
 */
import java.awt.Color;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.JFileChooser;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpinnerModel;

public class Menu extends javax.swing.JFrame {

    public static Menu menu = null;
    public static Select select = null;
    public static Editor editor = null;
    public static BufferedWriter writer;
    public static FileWriter outFile;
    public static PrintWriter out;

    /** Creates new form Menu */
    public Menu() {
        initComponents();
        setBackground(Color.WHITE);
        setTitle("RothRPG Map Editor - Launcher");
        setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        loadButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        fileField = new javax.swing.JTextField();
        logField = new javax.swing.JLabel();
        createButton = new javax.swing.JButton();
        selectButton = new javax.swing.JButton();
        SpinnerModel sm = new SpinnerNumberModel(10, 10, 255, 1);
        width = new javax.swing.JSpinner(sm);
        SpinnerModel sm2 = new SpinnerNumberModel(7, 7, 255, 1);
        height = new javax.swing.JSpinner(sm2);
        jButton4 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        mapName = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("RothRPG Map Editor");
        jLabel1.setName("jLabel1"); // NOI18N

        loadButton.setText("Load");
        loadButton.setName("loadButton"); // NOI18N
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadButtonActionPerformed(evt);
            }
        });

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel2.setText("Create new...");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Load existing file...");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("Size:");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("x");
        jLabel5.setName("jLabel5"); // NOI18N

        fileField.setName("fileField"); // NOI18N

        logField.setText("Must be 10x7 or bigger. Maximum size is 255x255 tiles.");
        logField.setName("logField"); // NOI18N

        createButton.setText("Create");
        createButton.setName("createButton"); // NOI18N
        createButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createButtonActionPerformed(evt);
            }
        });

        selectButton.setText("Select...");
        selectButton.setName("selectButton"); // NOI18N
        selectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectButtonActionPerformed(evt);
            }
        });

        width.setName("width"); // NOI18N
        width.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                widthKeyPressed(evt);
            }
        });

        height.setName("height"); // NOI18N

        jButton4.setText("Exit");
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel7.setText("by simontomi");
        jLabel7.setName("jLabel7"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        mapName.setText("map name.txt");
        mapName.setName("mapName"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 221, Short.MAX_VALUE)
                        .addComponent(jLabel7))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(fileField, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addGap(256, 256, 256))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(logField)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(10, 10, 10)
                                                .addComponent(jLabel4)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(width, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel5))
                                            .addComponent(jLabel2))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(mapName, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                                            .addComponent(height, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(43, 43, 43)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(createButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(loadButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(selectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(mapName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(width, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(height, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(6, 6, 6)
                        .addComponent(logField))
                    .addComponent(createButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loadButton)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
        // TODO add your handling code here:
        //editorStart(new File(fileField.getText()));
        System.out.println(select.fileChooser.getSelectedFile());
        editorStart(select.fileChooser.getSelectedFile());
    }//GEN-LAST:event_loadButtonActionPerformed

    private void widthKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_widthKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_widthKeyPressed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        System.exit(0);
    }//GEN-LAST:event_jButton4ActionPerformed

    private void selectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectButtonActionPerformed
        // TODO add your handling code here:
        int returnVal = select.fileChooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = select.fileChooser.getSelectedFile();
            try {
                // What to do with the file, e.g. display it in a TextArea
                //Menu.menu.fileField.read( new FileReader( file.getAbsolutePath() ), null );
                fileField.read(new FileReader(file.getAbsolutePath()), null);
                Menu.menu.fileField.setText(file.getName());
            } catch (IOException ex) {
                System.out.println("Problem accessing file " + file.getAbsolutePath());
            }
        } else {
            System.out.println("File access cancelled by user.");
        }
    }//GEN-LAST:event_selectButtonActionPerformed

    private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
        // TODO add your handling code here:
        String name = mapName.getText();
        File file1;
        if (name.endsWith(".txt")) {
            file1 = new File("maps/" + name);
        } else {
            file1 = new File("maps/" + name + ".txt");
        }
        boolean file1e = file1.exists();

        if (file1e) {
            logField.setForeground(Color.RED);
            logField.setText("This file already exists!");
        } else if (name.equals("") || name.equals(" ") || name == null || name.trim().equals("") || name.startsWith(" ") || name.endsWith(" ")) {
            logField.setForeground(Color.RED);
            logField.setText("Space before and after the name is prohibited.");
        } else if ((Integer) width.getValue() > 255 || (Integer) width.getValue() < 10 || (Integer) height.getValue() > 255 || (Integer) height.getValue() < 7) {
            logField.setForeground(Color.RED);
            logField.setText("Must be 10x7 or bigger. Maximum size is 255x255 tiles.");
        } else {
            logField.setForeground(Color.BLACK);
            logField.setText("Map created.");
            System.err.println("Creating file: " + file1);
            try {
                file1.createNewFile();
                try {
                    //FileWriter outFile = new FileWriter(file1);

                    //writer = new BufferedWriter(new FileWriter(file1, true));
                    //PrintWriter out = new PrintWriter(new FileWriter(file1, true));

                    //PrintWriter out = new PrintWriter(outFile);
                    outFile = new FileWriter(file1);
                    out = new PrintWriter(outFile);
                    //createLayer0();
                    //createLayer1();
                    //createLayer2();
                    //createLayer3();
                    //createLayer4();
                    //createLayer5();
                    createLayers();
                    //out.close();
                    System.err.println("BufferedWriter started. Writing into: " + file1);
                } catch (IOException ex) {
                    System.out.println("Failed to create BufferedWriter.");
                }
            } catch (IOException ex) {
                System.out.println("Could not create new map.");
            }
            this.setVisible(false);
            editorStart(file1);
        }
    }//GEN-LAST:event_createButtonActionPerformed

    public static void createLayers() {
        out.println(width.getValue());
        out.println(height.getValue());
        for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
            for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                if (n == 0) {
                    out.print("~");
                } else if (i == 0) {
                    out.print("\r\n~");
                } else {
                    out.print("~");
                }
            }
            if (n == (Integer) height.getValue() - 1) {
                out.println("\r\n--");
            }
            //out.println("\n");
        }
        for (int nu = 1; nu <= 4; nu++) {
            for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
                for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                    if (n == 0) {
                        out.print(".");
                    } else if (i == 0) {
                        out.print("\r\n.");
                    } else {
                        out.print(".");
                    }
                }
                if (n == (Integer) height.getValue() - 1) {
                    out.println("\r\n--");
                }
                //out.println("\n");
            }
        }
        out.close();
    }

    public static void createLayer0() throws IOException {

        out.println(width.getValue());
        out.println(height.getValue());
        for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
            for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                if (n == 0) {
                    out.print("~");
                } else if (i == 0) {
                    out.print("\r\n~");
                } else {
                    out.print("~");
                }
            }
            if (n == (Integer) height.getValue() - 1) {
                out.println("\r\n--");
            }
            //out.println("\n");
        }
        out.close();
    }

    public static void createLayer1() throws IOException {
        for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
            for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                if (n == 0) {
                    out.print(".");
                } else if (i == 0) {
                    out.print("\r\n.");
                } else {
                    out.print(".");
                }
            }
            if (n == (Integer) height.getValue() - 1) {
                out.println("\r\n--");
            }
            //out.println("\n");
        }
        out.close();
    }

    public static void createLayer2() throws IOException {
        for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
            for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                if (n == 0) {
                    out.print(".");
                } else if (i == 0) {
                    out.print("\r\n.");
                } else {
                    out.print(".");
                }
            }
            if (n == (Integer) height.getValue() - 1) {
                out.println("\r\n--");
            }
            //out.println("\n");
        }
        out.close();
    }

    public static void createLayer3() throws IOException {
        for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
            for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                if (n == 0) {
                    out.print(".");
                } else if (i == 0) {
                    out.print("\r\n.");
                } else {
                    out.print(".");
                }
            }
            if (n == (Integer) height.getValue() - 1) {
                out.println("\r\n--");
            }
            //out.println("\n");
        }
        out.close();
    }

    public static void createLayer4() throws IOException {
        for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
            for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                if (n == 0) {
                    out.print(".");
                } else if (i == 0) {
                    out.print("\r\n.");
                } else {
                    out.print(".");
                }
            }
            if (n == (Integer) height.getValue() - 1) {
                out.println("\r\n--");
            }
            //out.println("\n");
        }
        out.close();
    }

    public static void createLayer5() throws IOException {
        for (int n = 0; n <= (Integer) height.getValue() - 1; n++) {
            for (int i = 0; i <= (Integer) width.getValue() - 1; i++) {
                if (n == 0) {
                    out.print(".");
                } else if (i == 0) {
                    out.print("\r\n.");
                } else {
                    out.print(".");
                }
            }
            if (n == (Integer) height.getValue() - 1) {
                out.println("\r\n--");
            }
            //out.println("\n");
        }
        out.close();
    }

    public static void editorStart(File file1) {
        editor = new Editor(file1);
        editor.setVisible(true);
        editor.setTitle("RothRPG Map Editor - Editor");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        menu = new Menu();
        select = new Select();
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Menu().setVisible(true);
                //menu = new Menu();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static javax.swing.JButton createButton;
    public static javax.swing.JTextField fileField;
    public static javax.swing.JSpinner height;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    public static javax.swing.JButton loadButton;
    public static javax.swing.JLabel logField;
    public static javax.swing.JTextField mapName;
    private javax.swing.JButton selectButton;
    public static javax.swing.JSpinner width;
    // End of variables declaration//GEN-END:variables
}
